package List;

import java.util.Arrays;

//顺序表的自我实现
public class MyArrayList implements IFunction {
    //顺序表的构成 - 数组 + 记录数组中已有的元素个数
    private int[] array;
    private int arrayNumber;

    public static final int CAPACITY = 10;

    //构造方法
    public MyArrayList() {
        this.array = new int[CAPACITY];
        this.arrayNumber = 0;
    }

    @Override
    public void add(int data) {
        if (isFull()) {
            beBig();
        }
        this.array[this.arrayNumber] = data;
        this.arrayNumber++;
    }

    public boolean isFull() {
        return this.array.length == this.arrayNumber;
    }

    private void beBig() {
        this.array = Arrays.copyOf(this.array, this.array.length * 2);
    }

    @Override
    public void display() {
        for (int i = 0; i < this.arrayNumber; i++) {
            System.out.print(this.array[i] + " ");
        }
    }

    @Override
    public void add(int pos, int data) throws IndexException {
        if (pos < 0 || pos > this.arrayNumber) {
            throw new IndexException("索引异常！！！");
        } else {
            if (isFull()) {
                beBig();
            } else {
                for (int i = this.arrayNumber - 1; i >= pos; i--) {
                    this.array[i + 1] = this.array[i];
                }
                this.array[pos] = data;
                this.arrayNumber++;
            }
        }

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.arrayNumber; i++) {
            if (this.array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.arrayNumber; i++) {
            if (this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws EmptyException,IndexException{
        if(isEmpty()) {
            throw new EmptyException("数组为空！！！");
        }
        if (pos < 0 || pos >= this.arrayNumber) {
            throw new IndexException("索引异常！！！");
        }
        return this.array[pos];
    }

    @Override
    public void set(int pos, int value) throws EmptyException,IndexException{
        if(isEmpty()){
            throw new EmptyException("数组为空！！！");
        }
        if(pos<0||pos>=this.arrayNumber){
            throw new IndexException("索引异常！！！");
        }
        this.array[pos] = value;
    }

    public boolean isEmpty() {
        return this.arrayNumber == 0;
    }

    @Override
    public void remove(int toRemove) throws EmptyException{
        if(isEmpty()){
            throw new EmptyException("数组为空！！！");
        }
        int index = indexOf(toRemove);
        if(index == -1){
            return;
        }
        for(int i = index;i<this.arrayNumber-1;i++){
            this.array[i] = this.array[i+1];
        }
        this.arrayNumber--;
    }

    @Override
    public int size() {
        return this.arrayNumber;
    }
}
